package com.skp.shaphan.clipboard;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.util.ArrayList;

// from:http://elliotth.blogspot.com/2005/01/copying-html-to-clipboard-from-java.html
public class HtmlSelection implements Transferable {

    private static ArrayList htmlFlavors = new ArrayList();



    static {

        try {

            htmlFlavors.add(new DataFlavor("text/html;class=java.lang.String"));

            htmlFlavors.add(new DataFlavor("text/html;class=java.io.Reader"));

            htmlFlavors.add(new DataFlavor("text/html;charset=unicode;class=java.io.InputStream"));

        } catch (ClassNotFoundException ex) {

            ex.printStackTrace();

        }

    }



    private String html;



    public HtmlSelection(String html) {

        this.html = html;

    }



    public DataFlavor[] getTransferDataFlavors() {

        return (DataFlavor[]) htmlFlavors.toArray(new DataFlavor[htmlFlavors.size()]);

    }



    public boolean isDataFlavorSupported(DataFlavor flavor) {

        return htmlFlavors.contains(flavor);

    }



    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {

        if (String.class.equals(flavor.getRepresentationClass())) {

            return html;

        } else if (Reader.class.equals(flavor.getRepresentationClass())) {

            return new StringReader(html);

        } else if (InputStream.class.equals(flavor.getRepresentationClass())) {

            return new StringBufferInputStream(html);

        }

        throw new UnsupportedFlavorException(flavor);

    }

} 
